<?
use adminmatrix\migration\Migrator;
use  adminmatrix\migration\db\Column;

class Permission extends Migrator
{
    /**
         * Change Method.
         *
         * Write your reversible migrations using this method.
         *
         * More information on writing migrations is available here:
         * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
         *
         * The following commands can be used in this method and Phinx will
         * automatically reverse them when rolling back:
         *
         *    createTable
         *    renameTable
         *    addColumn
         *    renameColumn
         *    addIndex
         *    addForeignKey
         *
         * Remember to call "create()" or "update()" and NOT "save()" when working
         * with the Table class.
         */
    public function up()
    {
        $this->createRoleTable();
        $this->createPermissionTable();
        $this->createUserRoleTable();
        $this->createRolePermissionTable();
    }

    public function down()
    {
        $this->table('admin_role')->drop()->save();
        $this->table('admin_permission')->drop()->save();
        $this->table('admin_user_role')->drop()->save();
        $this->table('admin_role_permission')->drop()->save();
    }

    private function createRoleTable()
    {
        $table = $this->table('admin_role', [
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_general_ci',
            'comment' => '角色表',
            'id' => 'id',
            'primary_key' => ['id']
        ]);

        $table
            ->addColumn('name', 'string', [
                'null' => false,
                'limit' => 50,
                'comment' => '角色名称'
            ])
            ->addColumn('description', 'string', [
                'null' => true,
                'limit' => 255,
                'comment' => '角色描述'
            ])
            ->addColumn('status', 'tinyinteger', [
                'null' => true,
                'limit' => 1,
                'comment' => '状态 1：开启 0 关闭'
            ])
            ->addColumn('create_time', 'integer', [
                'null' => true,
                'default' => NULL,
                'limit' => 11,
                'comment' => '创建时间'
            ]);

        $table->addIndex('name', [
            'name' => 'name',
            'unique' => true
        ]);

        $table->create();
    }

    private function createPermissionTable()
    {
        $table = $this->table('admin_permission', [
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_general_ci',
            'comment' => '权限表',
            'id' => 'id',
            'primary_key' => ['id']
        ]);

        $table
            ->addColumn('name', 'string', [
                'null' => false,
                'limit' => 50,
                'comment' => '权限名称'
            ])
            ->addColumn('parent_id', 'integer', [
                'null' => false,
                'default' => 0,
                'limit' => 11,
                'comment' => '父级id',
            ])
            ->addColumn('component', 'string', [
                'null' => true,
                'default' => NULL,
                'limit' => 255,
                'comment' => '组件地址',
            ])
            ->addColumn('type', 'tinyinteger', [
                'null' => false,
                'default' => 1,
                'limit' => 1,
                'comment' => '菜单类型',
            ])
            ->addColumn('path', 'string', [
                'null' => true,
                'default' => NULL,
                'limit' => 255,
                'comment' => '路由地址',
            ])
            ->addColumn('icon', 'string', [
                'null' => true,
                'default' => NULL,
                'limit' => 255,
                'comment' => '图标',
            ])
            ->addColumn('perms', 'string', [
                'null' => false,
                'default' => NULL,
                'limit' => 255,
                'comment' => '权限地址',
            ])
            ->addColumn('hidden', 'tinyinteger', [
                'null' => false,
                'default' => 1,
                'limit' => 1,
                'comment' => '是否隐藏菜单',
            ])
            ->addColumn('create_time', 'integer', [
                'null' => true,
                'default' => NULL,
                'limit' => 11,
                'comment' => '创建时间',
            ])
            ->addColumn('update_time', 'integer', [
                'null' => true,
                'default' => NULL,
                'limit' => 11,
                'comment' => '编辑时间',
            ])
            ->addColumn('delete_time', 'integer', [
                'null' => true,
                'default' => NULL,
                'limit' => 11,
                'comment' => '删除时间',
            ]);

        $table->addIndex('name', [
            'name' => 'name',
            'unique' => false
        ]);

        $table->create();

        if ($this->hasTable('admin_permission')) {
            $menuData = [
                ['id' => 1, 'name' => '系统', 'parent_id' => 0, 'component' => '', 'type' => 1, 'path' => 'system', 'icon' => 'arco-icon-IconDesktop', 'perms' => '', 'hidden' => 0, 'create_time' => 1744508605, 'update_time' => 1744536826, 'delete_time' => null],
                ['id' => 2, 'name' => '工作台', 'parent_id' => 1, 'component' => 'system/workbench/index', 'type' => 2, 'path' => 'workbench', 'icon' => 'arco-icon-IconDesktop', 'perms' => 'workbench', 'hidden' => 0, 'create_time' => 1744508605, 'update_time' => 1744700678, 'delete_time' => null],
                ['id' => 3, 'name' => '权限管理', 'parent_id' => 1, 'component' => '', 'type' => 1, 'path' => 'auth', 'icon' => 'arco-icon-IconUserGroup', 'perms' => '', 'hidden' => 0, 'create_time' => 1744508605, 'update_time' => 1744536772, 'delete_time' => null],
                ['id' => 4, 'name' => '菜单权限', 'parent_id' => 3, 'component' => 'system/auth/permission/index', 'type' => 2, 'path' => 'permission', 'icon' => 'arco-icon-IconPublic', 'perms' => '/adminapi/system.auth.Permission/lists', 'hidden' => 0, 'create_time' => 1744508605, 'update_time' => 1744700726, 'delete_time' => null],
                ['id' => 5, 'name' => '新增', 'parent_id' => 4, 'component' => '', 'type' => 3, 'path' => null, 'icon' => '', 'perms' => '/adminapi/system.auth.Permission/save', 'hidden' => 0, 'create_time' => 1744508605, 'update_time' => 1744508605, 'delete_time' => null],
                ['id' => 6, 'name' => '编辑', 'parent_id' => 4, 'component' => '', 'type' => 3, 'path' => null, 'icon' => '', 'perms' => '/adminapi/system.auth.Permission/update', 'hidden' => 0, 'create_time' => 1744508649, 'update_time' => 1744508649, 'delete_time' => null],
                ['id' => 7, 'name' => '删除', 'parent_id' => 4, 'component' => '', 'type' => 3, 'path' => null, 'icon' => '', 'perms' => '/adminapi/system.auth.Permission/delete', 'hidden' => 0, 'create_time' => 1744508756, 'update_time' => 1744508756, 'delete_time' => null],
                ['id' => 8, 'name' => '管理员', 'parent_id' => 3, 'component' => 'system/auth/admin/index', 'type' => 2, 'path' => 'admin', 'icon' => 'arco-icon-IconUserAdd', 'perms' => '/adminapi/system.auth.admin_user/lists', 'hidden' => 0, 'create_time' => 1744538160, 'update_time' => 1744700743, 'delete_time' => null],
                ['id' => 9, 'name' => '新增', 'parent_id' => 8, 'component' => '', 'type' => 3, 'path' => null, 'icon' => '', 'perms' => '/adminapi/system.auth.admin_user/save', 'hidden' => 0, 'create_time' => 1744538277, 'update_time' => 1744538277, 'delete_time' => null],
                ['id' => 10, 'name' => '更新', 'parent_id' => 8, 'component' => '', 'type' => 3, 'path' => null, 'icon' => '', 'perms' => '/adminapi/system.auth.admin_user/update', 'hidden' => 0, 'create_time' => 1744538291, 'update_time' => 1744538301, 'delete_time' => null],
                ['id' => 11, 'name' => '删除', 'parent_id' => 8, 'component' => '', 'type' => 3, 'path' => null, 'icon' => '', 'perms' => '/adminapi/system.auth.admin_user/delete', 'hidden' => 0, 'create_time' => 1744538347, 'update_time' => 1744538347, 'delete_time' => null],
                ['id' => 12, 'name' => '角色', 'parent_id' => 3, 'component' => 'system/auth/role/index', 'type' => 2, 'path' => 'role', 'icon' => 'arco-icon-IconUser', 'perms' => '/adminapi/system.auth.role/lists', 'hidden' => 0, 'create_time' => 1744562021, 'update_time' => 1744700753, 'delete_time' => null],
                ['id' => 13, 'name' => '新增', 'parent_id' => 12, 'component' => '', 'type' => 3, 'path' => null, 'icon' => '', 'perms' => '/adminapi/system.auth.role/save', 'hidden' => 0, 'create_time' => 1744562033, 'update_time' => 1744562033, 'delete_time' => null],
                ['id' => 14, 'name' => '编辑', 'parent_id' => 12, 'component' => '', 'type' => 3, 'path' => null, 'icon' => '', 'perms' => '/adminapi/system.auth.role/update', 'hidden' => 0, 'create_time' => 1744562045, 'update_time' => 1744562045, 'delete_time' => null],
                ['id' => 15, 'name' => '删除', 'parent_id' => 12, 'component' => '', 'type' => 3, 'path' => null, 'icon' => '', 'perms' => '/adminapi/system.auth.role/delete', 'hidden' => 0, 'create_time' => 1744562056, 'update_time' => 1744562056, 'delete_time' => null],
                ['id' => 16, 'name' => '组织架构', 'parent_id' => 1, 'component' => '', 'type' => 1, 'path' => 'organize', 'icon' => 'arco-icon-IconUserGroup', 'perms' => '', 'hidden' => 0, 'create_time' => 1744617067, 'update_time' => 1744617067, 'delete_time' => null],
                ['id' => 17, 'name' => '组织', 'parent_id' => 16, 'component' => 'system/organize/company/index', 'type' => 2, 'path' => 'company', 'icon' => 'arco-icon-IconSubscribeAdd', 'perms' => '/adminapi/system.organize.company/lists', 'hidden' => 0, 'create_time' => 1744617184, 'update_time' => 1744700766, 'delete_time' => null],
                ['id' => 18, 'name' => '新增', 'parent_id' => 17, 'component' => '', 'type' => 3, 'path' => null, 'icon' => '', 'perms' => '/adminapi/system.organize.company/save', 'hidden' => 0, 'create_time' => 1744678905, 'update_time' => 1744678905, 'delete_time' => null],
                ['id' => 19, 'name' => '编辑', 'parent_id' => 17, 'component' => '', 'type' => 3, 'path' => null, 'icon' => '', 'perms' => '/adminapi/system.organize.company/update', 'hidden' => 0, 'create_time' => 1744678919, 'update_time' => 1744678984, 'delete_time' => null],
                ['id' => 20, 'name' => '删除', 'parent_id' => 17, 'component' => '', 'type' => 3, 'path' => null, 'icon' => '', 'perms' => '/adminapi/system.organize.company/delete', 'hidden' => 0, 'create_time' => 1744678929, 'update_time' => 1744678929, 'delete_time' => null],
                ['id' => 21, 'name' => '部门', 'parent_id' => 16, 'component' => 'system/organize/department/index', 'type' => 2, 'path' => 'department', 'icon' => 'arco-icon-IconBarChart', 'perms' => '/adminapi/system.organize.department/lists', 'hidden' => 0, 'create_time' => 1744678968, 'update_time' => 1744700774, 'delete_time' => null],
                ['id' => 22, 'name' => '新增', 'parent_id' => 21, 'component' => '', 'type' => 3, 'path' => null, 'icon' => '', 'perms' => '/adminapi/system.organize.department/save', 'hidden' => 0, 'create_time' => 1744685401, 'update_time' => 1744685401, 'delete_time' => null],
                ['id' => 23, 'name' => '编辑', 'parent_id' => 21, 'component' => '', 'type' => 3, 'path' => null, 'icon' => '', 'perms' => '/adminapi/system.organize.department/update', 'hidden' => 0, 'create_time' => 1744685416, 'update_time' => 1744685416, 'delete_time' => null],
                ['id' => 24, 'name' => '删除', 'parent_id' => 21, 'component' => '', 'type' => 3, 'path' => null, 'icon' => '', 'perms' => '/adminapi/system.organize.department/delete', 'hidden' => 0, 'create_time' => 1744685432, 'update_time' => 1744685432, 'delete_time' => null],
                ['id' => 25, 'name' => '职务', 'parent_id' => 16, 'component' => 'system/organize/position/index', 'type' => 2, 'path' => 'position', 'icon' => 'arco-icon-IconCodeSandbox', 'perms' => '/adminapi/system.organize.position/lists', 'hidden' => 0, 'create_time' => 1744685472, 'update_time' => 1744703303, 'delete_time' => null],
                ['id' => 26, 'name' => '详情', 'parent_id' => 8, 'component' => 'system/auth/admin/detail', 'type' => 2, 'path' => 'detail', 'icon' => '', 'perms' => 1, 'hidden' => 1, 'create_time' => 1744697437, 'update_time' => 1744697437, 'delete_time' => null],
                ['id' => 27, 'name' => '新增', 'parent_id' => 25, 'component' => '', 'type' => 3, 'path' => null, 'icon' => '', 'perms' => '/adminapi/system.organize.position/save', 'hidden' => 0, 'create_time' => 1744703352, 'update_time' => 1744703352, 'delete_time' => null]
            ];

            $this->table('admin_permission')->insert($menuData)->save();
        }
    }

    private function createUserRoleTable()
    {
        $table = $this->table('admin_user_role', [
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_general_ci',
            'comment' => '用户角色关联表',
            'id' => false
        ]);

        $table
            ->addColumn('user_id', 'integer', [
                'null' => false,
                'limit' => 11,
                'comment' => '用户 ID'
            ])
            ->addColumn('role_id', 'integer', [
                'null' => false,
                'limit' => 11,
                'comment' => '角色 ID'
            ]);

        $table->addIndex(['user_id', 'role_id'], [
            'name' => 'user_role_index',
            'unique' => false
        ]);

        $table->create();
    }

    private function createRolePermissionTable()
    {
        $table = $this->table('admin_role_permission', [
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_general_ci',
            'comment' => '角色权限关联表',
            'id' => false,
        ]);

        $table
            ->addColumn('role_id', 'integer', [
                'null' => false,
                'limit' => 11,
                'comment' => '角色 ID'
            ])
            ->addColumn('permission_id', 'integer', [
                'null' => false,
                'limit' => 11,
                'comment' => '权限 ID'
            ]);

        $table->addIndex(['role_id', 'permission_id'], [
            'name' => 'role_permission_index',
            'unique' => false
        ]);

        $table->create();
    }
}